package com.naiterui.ehp.bs.doctor.controller;


import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IProductService;
import com.naiterui.ehp.bs.doctor.vo.param.ProductSearchParam;
import com.naiterui.ehp.bs.doctor.vo.product.CategoryVO;
import com.naiterui.ehp.bs.doctor.vo.product.CueWordVO;
import com.naiterui.ehp.bs.doctor.vo.product.ProductDetailVO;
import com.naiterui.ehp.bs.doctor.vo.product.ProductInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author fwd
 */
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.MEDICINE_DRUG})
@RestController
@RequestMapping("ad/product")
public class AdProductController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdProductController.class);

    @Autowired
    private IProductService productService;


    /**
     * 搜索药品列表
     *
     * @param productSearch 搜索商品
     *
     * @return String <返回值描述>
     */
    @ApiOperation(value = "药品搜索", notes = "根据关键字搜索药品", httpMethod = "POST")
    @PostMapping("search")
    public ResponseVO<PageVO<ProductInfoVO>> search(ProductSearchParam productSearch) throws BusinessException {
        LOGGER.info("医生查询商品信息参数:{}", productSearch);

        if (productSearch == null || productSearch.getDoctorId() == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        PageVO<ProductInfoVO> productPage = this.productService.searchProduct(productSearch);

        return PublicService.returnResponseVO(productPage);
    }

    /**
     * 查询药品详情
     *
     * @param doctorId 医生ID
     *
     * @return String <返回值描述>
     */
    @ApiOperation(value = "获取商品详情", notes = "根据skuId获取商品详情", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "skuId", value = "skuId", required = true, dataType = "long", paramType = "query") })
    @PostMapping("detail")
    public ResponseVO<ProductDetailVO> detail(Long doctorId, Long skuId) throws BusinessException {
        LOGGER.info("医生ID:{} 获取商品详情skuId{}", doctorId, skuId);

        if (skuId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        return PublicService.returnResponseVO(this.productService.getProductDetailInfo(doctorId, skuId));
    }


    /**
     * 药品联想搜索
     *
     * @param key 搜索关键字
     *
     * @return String <返回值描述>
     */
    @ApiOperation(value = "获取商品提示词", notes = "医生搜索商品时，输入关键字时，下拉提示的药品列表", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "key", value = "关键字", required = true, dataType = "String", paramType = "query") })
    @GetMapping("cueWord")
    public ResponseVO<List<CueWordVO>> getCueWord(String key, Long doctorId) throws BusinessException {
        LOGGER.info("医生ID:{} 联想搜索词:{}", doctorId, key);

        if (StringUtils.isEmpty(key)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        List<CueWordVO> cueWord = this.productService.getCueWord(doctorId, key);

        return PublicService.returnResponseVO(cueWord);
    }

    /**
     * 查询商品分类
     *
     * @param doctorId 医生ID
     *
     * @return String <返回值描述>
     */
    @ApiOperation(value = "查询商品分类", notes = "获取商品分类信息", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query") })
    @GetMapping("category")
    public ResponseVO<List<CategoryVO>> getCategoryInfo(Long doctorId) throws BusinessException {
        LOGGER.info("医生ID:{} 获取商品详情", doctorId);

        if (doctorId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        List<CategoryVO> categoryList = this.productService.getCategoryInfo(doctorId);

        return PublicService.returnResponseVO(categoryList);
    }


}
